<?php
/**
 * Created by PhpStorm.
 * User: zq
 * Date: 2017/3/2
 * Time: 11:57
 */

namespace Home\Controller;


use Org\Util\Cart;
use Think\Controller;

class CartController extends Controller
{
    public function index(){
        $headConf = [
            'js'=>['list'],
            'css'=>['index'],
            'title'=>'上一天堂'
        ];
        $this->assign('headConf',$headConf);
//        p($_SESSION);
        $cartData = isset($_SESSION['cart']) ? $_SESSION['cart'] : [];
        $this->assign('cartData',$cartData);
        $this->display();
    }
    //异步修改购物车
    public function ajaxUpdateCart(){
        if(IS_AJAX){
//            p($_POST);
            $data=array(
                'sid'=>$_POST['sid'],// 唯一 sid，添加购物车时自动生成
                'num'=>$_POST['num']
            );
            (new Cart())->update($data);
        }
    }

    public function ajaxdelCart(){
        $sid = $_POST['sid'];
        $num =0;
        if(IS_AJAX){
            $data=array(
                'sid'=>$_POST['sid'],// 唯一 sid，添加购物车时自动生成
                'num'=>$num
            );
        }
        array_splice($_SESSION['cart']['goods'], $sid, 1);
        (new Cart())->update($data);
    }


}